/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Models.Profile;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author trang_000
 */
public class ProfileDAO {

    private Connection con;

    public ProfileDAO() {
        con = DBFactory.createConnection("dormitoryv2");
    }

    public boolean InsertProfile(Profile profile) {
        boolean result = false;
        try {
            String sqlst1 = "INSERT INTO profile (studentCode,address, idUser, phoneNo,email,Dob,status,gender,firstName, lastName) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

            PreparedStatement ps = con.prepareStatement(sqlst1);
            ps.setString(1, profile.getCodeStudent());
            ps.setString(2, profile.getAddress());
            if (profile.getUser() != null) {
                ps.setInt(3, profile.getUser().getUserID());
            } else {
                ps.setInt(3, 0);
            }
            ps.setString(4, profile.getPhoneNumber());
            ps.setString(5, profile.getEmail());
            ps.setDate(6, new java.sql.Date(profile.getBirthdate().getTime()));
            ps.setInt(7, profile.getStatus());
            ps.setInt(8, profile.getGender());
            ps.setString(9, profile.getFirstName());
            ps.setString(10, profile.getLastName());
            ps.executeUpdate();
            result = true;
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public Profile getProfileByProfileID(int profileID) {
        Profile profile = new Profile();
        String sqlcommand = "SELECT * FROM profile  where profileID = ? ";

        try {
            PreparedStatement ps = con.prepareStatement(sqlcommand);
            ps.setInt(1, profileID);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                profile.setProfileID(rs.getInt(1));
               // profile.setUserID(rs.getInt(2));
                profile.setCodeStudent(rs.getString(3));
                profile.setFirstName(rs.getString(4));
                profile.setLastName(rs.getString(5));
                profile.setGender(rs.getInt(6));
                profile.setBirthdate(rs.getDate(7));
               // profile.setUniversityID(rs.getInt(8));
                profile.setAddress(rs.getString(9));
                profile.setPhoneNumber(rs.getString(10));
                profile.setStatus(rs.getInt(11));
                profile.setCreateDate(rs.getDate(12));
               // profile.setRoomID(rs.getInt(13));

                //results.add(profile);
            }

        } catch (SQLException ex) {

        }

        return profile;

    }
}
